package com.lak;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Application {
    public static void main(String[] args) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        Random random = new Random();

        System.out.println("正在加载测试文字...");
        List<TestWord> testWords = mapper.readValue(new File("src/main/resources/words.json"), new TypeReference<>() {
            @Override
            public Type getType() {
                return super.getType();
            }
        });
        System.out.println("测试文字加载完毕...");
        System.out.println("请选择测试类型：1. 文字测试 2. 全码测试 (输入:q退出程序)");
        Scanner sc = new Scanner(System.in);
        String s;
        while ((s = sc.nextLine()) != null) {
            if ("1".equals(s)) {
                System.out.println("开始文字测试... (输入:q即可重新选择测试类型，输入:s即可显示正确答案)");
                boolean exit = false;
                while (true) {
                    random.setSeed(System.currentTimeMillis());
                    int randIdx = random.nextInt(testWords.size());
                    TestWord testWord = testWords.get(randIdx);
                    System.out.println(testWord.word);
                    String in;
                    while ((in = sc.nextLine()) != null) {
                        if (":q".equals(in)) {
                            exit = true;
                            break;
                        } else if (":s".equals(in)) {
                            System.out.println("显示答案，其全码为：" + testWord.qm);
                            break;
                        } else if (testWord.word.equals(in)) {
                            System.out.println("正确！其全码为：" + testWord.qm);
                            break;
                        } else {
                            System.out.println("错误，请重新输入... 输入:s即可显示正确答案");
                            System.out.println(testWord.word);
                        }
                    }
                    if (exit) {
                        break;
                    }
                }
            } else if ("2".equals(s)) {
                System.out.println("开始全码测试... (输入:q即可重新选择测试类型，输入:s即可显示正确答案)");
                boolean exit = false;
                while (true) {
                    random.setSeed(System.currentTimeMillis());
                    int randIdx = random.nextInt(testWords.size());
                    TestWord testWord = testWords.get(randIdx);
                    System.out.println(testWord.word);
                    String in;
                    while ((in = sc.nextLine()) != null) {
                        if (":q".equals(in)) {
                            exit = true;
                            break;
                        } else if (":s".equals(in)) {
                            System.out.println("显示答案，其全码为：" + testWord.qm);
                            break;
                        } else if (testWord.qm.equals(in)) {
                            System.out.println("正确！其全码为：" + testWord.qm);
                            break;
                        } else {
                            System.out.println("错误，请重新输入... 输入:s即可显示正确答案");
                            System.out.println(testWord.word);
                        }
                    }
                    if (exit) {
                        break;
                    }
                }
            } else if (":q".equals(s)) {
                break;
            } else {
                System.out.println("不支持的测试类型...请重新选择...");
            }
            System.out.println("请选择测试类型：1. 文字测试 2. 全码测试");
        }
    }
}
